import {
	ref,
	watch
} from 'vue';

export default function useAudio() {
	const innerAudioContext = uni.createInnerAudioContext()
	
	const isPlayWord = ref(false)
	
	innerAudioContext.onStop(() => {
		console.log('音乐停了', isPlayWord.value)
		isPlayWord.value = false
	})
	innerAudioContext.onEnded(() => {
		console.log('音乐停了', isPlayWord.value)
		isPlayWord.value = false
	})
	innerAudioContext.onPlay(() => {
		console.log('音乐开始播放')
	})
	
	function playAudio(url){
		if(!isPlayWord.value){
			isPlayWord.value = true
			innerAudioContext.stop()
			innerAudioContext.src = url
			innerAudioContext.autoplay = true;
			innerAudioContext.play()
		} else{
			isPlayWord.value = false
			innerAudioContext.stop()
		}
	}
	
	function stopAudio(){
		isPlayWord.value = false
		innerAudioContext.stop()
	}
	return { playAudio, isPlayWord, stopAudio }
}